package calao.util.xml;

import java.util.HashMap;

class DefaultMap<T extends Enum> implements NameMap<T> {

	final HashMap<String,T> enums = new HashMap<String,T>();
	final HashMap<T,String> names = new HashMap<T,String>();
	
	DefaultMap(Class<T> jclass)
	{
		for (T enumConstant : jclass.getEnumConstants())
		{
			String name = buildName(enumConstant);
			enums.put(name, enumConstant);
			names.put(enumConstant, name);
		}
	}
	
	public T getEnum(String name) {
		return enums.get(name);
	}

	public String getName(T enumValue) {
		return names.get(enumValue);
	}
	
	String buildName(T enumConstant)
	{
		String jname = enumConstant.name();
		StringBuffer buffer = new StringBuffer();
		for (String token : jname.split("_")) {
			if (buffer.length()>0) buffer.append("-");
			buffer.append(token.toLowerCase());
		}
		return buffer.toString();
	}

}
